//订单分佣流水
import React from 'react'
import { Modal, Form, Input, Button, Space, InputNumber, message, Descriptions, Row, Col } from "antd";
import TheTable from '@/components/TheTable'
import ThePagination from '@/components/ThePagination'
import TheButton from '@/components/TheButton'
import TheQueryForm from '@/components/TheQueryForm';
import { apiGetAppealPage } from '@/request/riskControl'
import Json2Excel from '@/utils/Json2Excel'
import TheImage from '@/components/TheImage';
import enums from "../Enum.js"
import { getDateTime2 } from '@/utils/getDate'
import moment from 'moment';
import HandleDialog from '../components/handleDialog'
import store from "@/store/store"
import _ from 'lodash'
// 获取当前年份和月份
const currentYear = moment().year();
const currentMonth = moment().month();
const currentDate = moment().date();
//处理年月为上一个月
let year = '', month = ''
if (currentMonth === 0) {
  year = currentYear - 1
  month = 12
} else {
  year = currentYear
  month = currentMonth - 1
}
export default class Appeal extends React.Component {
  handleRefs = React.createRef()

  state = {
    searchOption: [
      { field: 'applicantId', label: '申诉人ID', placeholder: '请输入', type: 'Input', defaultValue: '' },
      {
        field: 'time', label: '申诉时间', type: 'RangePicker',
        otherConfig: {
          showTime: true,
          disabledDate: (current) => {
            // 获取要选择的年份和月份
            const selectedYear = current.year();
            const selectedMonth = current.month();
            const selectedDate = current.date();
            // 如果要选择的年份大于当前年份，禁用它
            if (selectedYear > currentYear) {
              return true;
            }
            // 如果要选择的年份等于当前年份，且要选择的月份大于当前月份，禁用它
            if (selectedYear === currentYear && selectedMonth > currentMonth) {
              return true;
            }
            // 如果要选择的年份等于当前年份，且要选择的月份大于当前月份，禁用它
            if (selectedYear === currentYear && selectedMonth === currentMonth && selectedDate > currentDate) {
              return true;
            }
            // 允许选择该日期
            return false;
          }
        },
      },
      { field: 'appealType', label: '申诉类型', placeholder: '全部', type: 'Select', options: [] },
      { field: 'state', label: '状态', placeholder: '全部', type: 'Select', options: [{ label: '全部', value: '' }, { label: '待处理', value: '0' }, { label: '已处理', value: '1' }] },
    ],
    columns: [
      {
        title: "id",
        dataIndex: "appealId",
        key: "appealId",
      },
      {
        title: "申诉时间",
        dataIndex: "createTime",
        key: "createTime",
      },
      {
        title: "申诉人ID",
        dataIndex: "applicantId",
        key: "applicantId",
      },
      {
        title: "申诉人昵称",
        dataIndex: "applicantName",
        key: "applicantName",
      },
      {
        title: "申诉类型",
        dataIndex: "appealType",
        key: "appealType",
        render: (text, _) => {
          const { reportState, reportState_content } = enums
          const typeArr = Object.assign({}, reportState, reportState_content)
          return typeArr[text]
        }
      },
      {
        title: "状态",
        dataIndex: "state",
        key: "state",
        render: (text) => {
          let settlementStatus = ''
          switch (text) {
            case 0:
              settlementStatus = '待处理'
              break
            case 1:
              settlementStatus = '已处理'
              break
            default:
              settlementStatus = '-'
              break
          }
          return settlementStatus
        }
      },
      {
        title: '操作',
        align: 'center',
        fixed: "right",
        width: 200,
        render: record => {
          return (
            <>
              <TheButton haspermi={['riskControl:appeal:view']} onClick={() => this.goView(record.appealId,'appeal')}>查看</TheButton>
              {/* {record.state == 0 && <TheButton haspermi={['riskControl:appeal:dispose']} onClick={() => this.handleRefs.open({ id: record.appealId, mode: 'J', type: this.getType(record.appealType) ? "content" : 'user', from: "appeal" })}>去处理</TheButton> } */}
              {record.state == 0 && <TheButton haspermi={['riskControl:appeal:dispose']} onClick={() => this.goView(record.appealId,'appealDoing')}>去处理</TheButton> }

            </>
          )
        }
      }
    ],
    dataSource: [],
    pageData: { pageNo: 1, pageSize: 10 },
    total: 0,
    exportBtnLoading: false,
  }
  componentDidMount() {
    let searchParams = store.getState().searchValue
    const { searchOption } = this.state
    const { reportState, reportState_content } = enums
    const typeArr = Object.assign({}, reportState, reportState_content)
    searchOption.map(item => {
      if (item.field == 'appealType') {
        item.options = Object.keys(typeArr).map(obj => {
          return {
            label: typeArr[obj],
            value: obj
          }
        })
      }
      return item
    })
    let { pageNo, pageSize, queryForm, pathName } = searchParams
    if (searchParams && pageNo && pathName == 'riskControlAppeal') {
      this.setState({
        queryForm, pageData: { pageNo, pageSize }
      }, () => {
        this.queryFormRef.onSetDefault({ ...queryForm })
        this.getData({ ...queryForm })
      })
    } else {
      this.getData()
    }
  }
  getType = (val) => {
    const { reportState_content } = enums
    return Object.keys(reportState_content).includes(val)
  }
  //数据处理
  getData = (values) => {
    let { pageData, queryForm } = this.state
    this.setState({
      queryForm: values === undefined ? this.state.queryForm : { ...values }
    }, async () => {
      let params = { ...pageData, queryForm }
      store.dispatch({
        type: 'riskControlAppeal',
        data: { pathName: 'appeal', ...params }
      })
      await apiGetAppealPage({ ...pageData, ...this.state.queryForm }).then(res => {
        if (res.code === 0) {
          this.setState({
            dataSource: res.data,
            pageData: {
              pageNo: res.pageNo,
              pageSize: res.pageSize
            },
            total: res.totalCount
          })
        }
      })
    })
  }
  //分页
  pageChange = (pageNo, pageSize) => {
    this.setState({ pageData: { pageNo, pageSize } }, () => {
      this.getData(this.state.queryForm)
    })
  }

  goView = (id,way) => {
    this.props.history.push(`/riskControl/${way}/view/${id}`)
  }

  //搜索
  onFormSearch(values) {
    if (values && !_.isEmpty(values.time)) {
      values.startTime = getDateTime2(values.time[0])
      values.endTime = getDateTime2(values.time[1])
      delete values.time
    }
    if (values && !_.isEmpty(values.address)) {
      values.address = values.address.join('')
    }
    this.setState({
      pageData: { pageNo: 1, pageSize: 10 }, year, month
    }, () => {
      this.getData(values)
    })
  }
  //搜索重置
  onReset() {
    this.setState({ queryForm: {} }, () => {
      this.pageChange(1, 10)
    })
  }
  // 导出
  export = () => {

  }
  render() {
    const { searchOption, columns, dataSource, pageData: { pageNo, pageSize }, total, exportBtnLoading } = this.state
    return (
      <div className='p-layout-box commodity-content'>
        <TheQueryForm
          ref={(ref) => this.queryFormRef = ref}
          option={searchOption}
          show={true}
          onReset={() => this.onReset()}
          onGetValues={(values) => this.onFormSearch(values)} >
        </TheQueryForm>

        {/* <div style={{ display: 'flex', justifyContent: 'flex-end', marginBottom: 20 }}>
          <TheButton haspermi={['distribution:orderCommission:export']} size='middle' type='default' onClick={() => { this.export() }} loading={exportBtnLoading}>导出</TheButton>
        </div> */}
        <TheTable
          scroll={{ x: "max-content", y: '63vh' }}
          columns={columns}
          dataSource={dataSource}
          rowKey={'userId'}
        />
        <ThePagination pageNo={pageNo} pageSize={pageSize} total={total} pageChange={this.pageChange} />
        <HandleDialog ref={ref => this.handleRefs = ref} getData={this.getData}></HandleDialog>
      </div>
    )
  }
}
